<?php

namespace addons\booking\controller;
use app\common\library\Sms;
use think\Db;
/**
 * 内容
 */
class Content  extends  Base
{
    protected $noNeedLogin = ['notice','getBanner','knowledge','knowledge_detail','service_term','notice_detail','getTrends','evaluateList'];

    /**
     * 获取平台公告
     */

    public function  notice(): \think\response\Json
    {
        if ($this->request->isGet()) {
            $page_type=$this->request->param('page_type',1);
            $page=$this->request->param('page',1);
            $limit=$this->request->param('limit',10);
            $list=DB::table('nn_content_notice')
                ->where("FIND_IN_SET($page_type,page_type)")
                ->where('status','1')
                ->field('id,title,image,page_type,start_time,end_time,areas,createtime')
                ->order('weigh','desc')
                ->paginate($limit,false,['page'=>$page]);
            if(!$list){
                $this->error('为找到相关信息');
            }
            $this->success('成功',$list);
        }
        $this->error('请求错误');
    }
    /**
     * 获取平台公告
     */
    public function notice_detail()
    {
        if($this->request->isGet()){
            $notice_id=$this->request->param('notice_id');
            $notice_id=intval(trim($notice_id));
            $detail=DB::table('nn_content_notice')
                ->field('id,title,image,content,start_time,end_time,createtime')
                ->where('status',1)
                ->find($notice_id);
            if(!$detail){
                $this->error('为找到相关信息');
            }
            $this->success('成功',$detail);
        }
        $this->error('请求错误');
    }
    /**
     * 获取banner
     */
    public function  getBanner(): \think\response\Json
    {
        if ($this->request->isGet()) {
            $page_type=$this->request->param('page_type','');
            $page_type=intval(trim($page_type));
            $list=DB::table('nn_content_banner')
                ->where('status','1')
                ->where("FIND_IN_SET($page_type,page_type)")
                ->field('id,title,image,page_type,type,url,url_type,start_time,end_time,areas,createtime')
                ->order('weigh','desc')
                ->limit('','20')->select();
            if(!$list){
                $this->error('为找到相关信息');
            }

            $this->success('成功',$list);
        }
        $this->error('请求错误');
    }
    /**
     * 接单动态
     */
    public function  getTrends(): \think\response\Json
    {
        if ($this->request->isGet()) {
            $type=$this->request->param('type',1);
            $page_type=intval(trim($type));
            $list=DB::table('nn_content_trends')->alias('a')
                ->join('nn_worker b','a.worker_id=b.id' ,'left')
                ->join('nn_booking_store c', 'a.store_id=c.id','left')
                ->field('a.type,b.user_name,c.name')
                ->where(['a.status'=>1,'a.type'=>$type])
                ->order('a.weigh', 'desc')
                ->limit(50)
                ->select();
            if(!$list){
                $this->error('未找到相关信息');
            }
            $this->success('成功',$list);
        }
    }
    /**
     * 知识管理
     */
    public function knowledge(): \think\response\Json
    {
        if ($this->request->isGet()) {
            $page_type=$this->request->param('page_type','');
            $page_type=intval(trim($page_type));
            $pageNum = 20;
            if (isset($param['page']) && !empty($param['num'])) {
                $pageNum = $param['num'];
            }
            $list=DB::table('nn_content_knowledge')
                ->field('id,title,image,describe,color,page_type,content,createtime')
                ->where('page_type',$page_type)
                ->where('status',1)
                ->order('weigh','desc')
                ->paginate($pageNum);
            if(!$list){
                $this->error('为找到相关信息');
            }
            $this->success('成功',$list);
        }
        $this->error('请求错误');
    }
    /**
     * 知识详情
     */
    public function knowledge_detail()
    {
        if($this->request->isGet()){
            $knowledge_id=$this->request->param('knowledge_id');
            $knowledge_id=intval(trim($knowledge_id));
            $detail=DB::table('nn_content_knowledge')
                ->field('id,title,image,describe,color,page_type,content')
                ->where('status',1)
                ->find($knowledge_id);
            if(!$detail){
                $this->error('为找到相关信息');
            }
            $this->success('成功',$detail);
        }
        $this->error('请求错误');
    }
    /**
     * 服务条款
     */
    public function service_term(): \think\response\Json
    {
        if($this->request->isPost()){
            $service_term_id=$this->request->param('service_term_id');
            $service_term_id=intval(trim($service_term_id));
            $list=DB::table('nn_content_service_term')
                ->field('id,title,content,createtime')
                ->where('status',1)
                ->find( $service_term_id);
            if(!$list){
                $this->error('为找到相关信息');
            }
            $this->success('成功',$list);
        }
        $this->error('请求错误');

    }
    /**
     * 违规申诉
     */
    public function  violationAppeal()
    {
        if($this->request->isPost()){
            $images = $this->request->param('images');
            $content = $this->request->param('content');
            $captcha = $this->request->param('captcha');
            $user = $this->auth->getUser();
            if(!$content) $this->error('请填写申诉描述');
            if (!Sms::check($user['mobile'], $captcha, 'mobilelogin')) {
                $this->error(__('Captcha is incorrect'));
            }
            $data=[
                'user_id' =>$this->auth->id,
                'images'   =>$images,
                'content' =>$content,
                'createtime'=>time()
            ];
            DB::startTrans();
            try{
                Db::name('appeal')->insert($data);
                $res = 1;
                $msg = '添加成功';
                Db::commit();
            }catch (\Exception $e){
                Db::rollback();
                $res = 2;
                $msg = '添加失败';
            }
            if($res == 1){
                $this->success($msg);
            } else {
                $this->error($msg);
            }

        }
    }
    /**
     * 意见反馈
     */
    public function  feedBack()
    {
        if($this->request->isPost()){
            $images = $this->request->param('images');
            $content=$this->request->param('content');
            if(!$content) $this->error('请填写您的意见');
            if(!$images) $this->error('请上传反馈图片');
            $data=[
                'user_id' =>$this->auth->id,
                'content' =>$content,
                'images' =>$images,
                'createtime'=>time()
            ];
            DB::startTrans();
            try{
                $feedBackId=Db::name('booking_feedback')->insertGetId($data);
                $res = 1;
                $msg = '添加成功';
                Db::commit();
            }catch (\Exception $e){
                Db::rollback();
                $res = 2;
                $msg = '添加失败';
            }
            if($res == 1){
                $this->success($msg);
            } else {
                $this->error($msg);
            }

        }
    }
    /**
     * 添加保洁评价
     */
    public function  addCleanEvaluate()
    {
        $score=$this->request->param('score');
        $content=$this->request->param('content');
        if(!$content) $this->error('请填写品论');
        if(!$score) $this->error('请打分');
        $images=$this->request->param('images');
        $data=[
            'order_id' =>1,
            'store_id' =>1,
            'store_user_id'=>1,
            'worker_id'=>1,
            'score'=>$score,
            'content'=>$content,
            'images'=>$images,
            'create_time'=>time()
        ];
        DB::startTrans();
        try {
            Db::name('worker_comment')->insertGetId($data);
            $res = 1;
            $msg = '添加成功';
            Db::commit();
        }catch (\Exception $e){
            Db::rollback();
            $res = 2;
            $msg = '添加失败';
        }
        if($res == 1){
            $this->success($msg);
        } else {
            $this->error($msg);
        }
    }
    /**
     * 保洁评价列表总条数
     */
    public function evaluateListCount()
    {
        $workID=1;
        $sql="SELECT count(*) AS 总数,count(score>3 OR NULL) AS 好评,count(score=3 OR NULL) AS 中评,count(score<3 OR NULL) AS 差评  FROM nn_worker_comment  where worker_id={$workID}";
        $res=DB::query($sql);
        $this->success('获取成功',$res);

    }
    /**
     * 保洁评价列表
     */
    public function evaluateList()
    {
        $workerID=1;
        $type=$this->request->param('type');
        switch ($type){
            case 1;//好评
                $where='score>3';
                break;
            case 2;//中评
                $where='score=3';
                break;
            case 3;//差评
                $where='score<3';
                break;
            default;
                $where='score>0';
        }
        $pageNum = 15;
        $param['num']=$this->request->param('num');
        if (isset($param['num']) && !empty($param['num'])) {
            $pageNum = $param['num'];
        }
        $list=DB::name('worker_comment')->alias('a')
            ->join('store_user b','a.store_user_id=b.id')
            ->join('booking_store c','a.store_id=c.id')
            ->field('a.id,a.score,a.content,a.images,a.create_time,b.user_name,b.image,c.name,c.logo')
            ->where('a.worker_id',$workerID)
            ->where($where)
            ->order('a.id','desc')
            ->paginate($pageNum)->toArray();
        foreach ($list['data'] as $key=>$v){
               $list['data'][$key]['new_images']=explode(',',rtrim($list['data'][$key]['images'],','));
               $list['data'][$key]['created_time']=formatTime($list['data'][$key]['create_time']);
        }
        $this->success('获取成功',$list);
    }

}
